using System;
using CrystalDecisions.CrystalReports.Engine;
using THANHTUNGO.Portal.Components.UI;
using ITG.Modules.HR.Components;
using THANHTUNGO.Portal.Components.Security;
using ITG.Modules.HR.Components.SmartBoss;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;

namespace ITG.Modules.HR.Controls
{
    public partial class TimeCard : YAFPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtFromDate.Text = txtToDate.Text = DateTime.Today.ToShortDateString();
                lstDepartment.DataSource = Department.GetAccessingDepartments(Convert.ToInt32(PortalSecurity.CurrentUser.Identity.ID));
                lstDepartment.DataBind();
                
            }
        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            ArrayList selectedItems = new ArrayList();
            foreach (ListItem item in lstDepartment.Items)
            {
                if (item.Selected)
                {
                    selectedItems.Add(item.Value);
                }
            }

            string[] departments = (string[]) selectedItems.ToArray(typeof(string));
            DataSet ds = Components.SmartBoss.TimeCard.GetBy(departments, DateTime.Parse(txtFromDate.Text), DateTime.Parse(txtToDate.Text));
            
            ReportDocument rpt = new ReportDocument();
            string path = Server.MapPath("~/Modules/ITG.HR/Reports/DailyChecking.rpt");
            rpt.Load(path);
            rpt.SetDataSource(ds.Tables[0]);

            rpt.SetParameterValue("@FromDate", DateTime.Parse(txtFromDate.Text));
            rpt.SetParameterValue("@ToDate", DateTime.Parse(txtToDate.Text));
            rpt.SetParameterValue("@ReportedBy", "tung.ngo@itg-phongphu.com");

            Session["HR.Report"] = rpt;
            string url = string.Format("~/Modules/ITG.HR/ViewReport.aspx?tabid={0}&mid={1}", this.TabID, this.ModuleID);
            Response.Redirect(url);
        }

        protected void btnMonthlyReport_Click(object sender, EventArgs e)
        {
            ArrayList selectedItems = new ArrayList();
            foreach (ListItem item in lstDepartment.Items)
            {
                if (item.Selected)
                {
                    selectedItems.Add(item.Value);
                }
            }

            string[] departments = (string[])selectedItems.ToArray(typeof(string));
            DataSet ds = Components.SmartBoss.TimeCard.GetBy(departments, DateTime.Parse(txtFromDate.Text), DateTime.Parse(txtToDate.Text));

            ReportDocument rpt = new ReportDocument();
            string path = Server.MapPath("~/Modules/ITG.HR/Reports/MonthlyChecking.rpt");
            rpt.Load(path);
            rpt.SetDataSource(ds.Tables[0]);
            rpt.SetParameterValue("@FromDate", DateTime.Parse(txtFromDate.Text));
            rpt.SetParameterValue("@ToDate", DateTime.Parse(txtToDate.Text));

            rpt.SetParameterValue("@ReportedBy", "tung.ngo@itg-phongphu.com");

            Session["HR.Report"] = rpt;
            string url = string.Format("~/Modules/ITG.HR/ViewReport.aspx?tabid={0}&mid={1}", this.TabID, this.ModuleID);
            Response.Redirect(url);
        }
    }
}